package com.ruoyi.web.controller.community;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.BbsArticleDetail;
import com.ruoyi.community.domain.BbsPunish;
import com.ruoyi.community.service.BbsArticleService;
import com.ruoyi.community.service.IBbsPunishService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 论坛惩罚Controller
 * 
 * @author ruoyi
 * @date 2021-08-16
 */
@RestController
@RequestMapping("/bbs/punish")
public class BbsPunishController extends BaseController
{
    @Autowired
    private IBbsPunishService bbsPunishService;
    @Autowired
    private BbsArticleService bbsArticleService;
    /**
     * 查询论坛惩罚列表
     */
    @PreAuthorize("@ss.hasPermi('system:punish:list')")
    @GetMapping("/list")
    public TableDataInfo list(BbsPunish bbsPunish)
    {
        startPage();
        List<BbsPunish> list = bbsPunishService.selectBbsPunishList(bbsPunish);
        return getDataTable(list);
    }

    /**
     * 导出论坛惩罚列表
     */
    @PreAuthorize("@ss.hasPermi('system:punish:export')")
    @Log(title = "论坛惩罚", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BbsPunish bbsPunish)
    {
        List<BbsPunish> list = bbsPunishService.selectBbsPunishList(bbsPunish);
        ExcelUtil<BbsPunish> util = new ExcelUtil<BbsPunish>(BbsPunish.class);
        return util.exportExcel(list, "论坛惩罚数据");
    }

    /**
     * 获取论坛惩罚详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:punish:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bbsPunishService.selectBbsPunishById(id));
    }

    /**
     * 新增论坛惩罚
     */
    @PreAuthorize("@ss.hasPermi('system:punish:add')")
    @Log(title = "论坛惩罚", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BbsPunish bbsPunish)
    {
        return toAjax(bbsPunishService.insertBbsPunish(bbsPunish));
    }

    /**
     * 修改论坛惩罚
     */
    @PreAuthorize("@ss.hasPermi('system:punish:edit')")
    @Log(title = "论坛惩罚", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BbsPunish bbsPunish)
    {
        return toAjax(bbsPunishService.updateBbsPunish(bbsPunish));
    }

    /**
     * 删除论坛惩罚
     */
    @PreAuthorize("@ss.hasPermi('system:punish:remove')")
    @Log(title = "论坛惩罚", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for (Long id : ids) {
            BbsPunish bbsPunish = bbsPunishService.selectBbsPunishById(id);
            Long articleId = bbsPunish.getArticleid();
            BbsArticleDetail bbsArticleDetail = new BbsArticleDetail();
            bbsArticleDetail.setArticleid(articleId);
            bbsArticleDetail.setDeleteflag(0);
            System.out.println(bbsArticleDetail);
            bbsArticleService.updateByArticleId(bbsArticleDetail);
        }
        return toAjax(bbsPunishService.deleteBbsPunishByIds(ids));
    }
    @GetMapping("/isPunish")
    public String isPunish(){
        Long id = SecurityUtils.getUserID();
        return bbsPunishService.isPunish(id);
    }

}
